using System;
using System.Windows.Forms;

namespace WegasManagementSystem.Forms
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            // 设置窗体标题
            this.Text = "高校工资管理系统 - 主窗口";
            
            // 显示登录用户信息
            if (GlobalConfig.CurrentUser != null)
            {
                statusLabel.Text = $"当前用户: {GlobalConfig.CurrentUser.Username} [{GlobalConfig.CurrentUser.UserRole}]";
            }
            
            // 根据用户角色设置权限
            SetPermissionsByRole();
        }

        private void SetPermissionsByRole()
        {
            // 如果没有登录用户，禁用所有功能
            if (GlobalConfig.CurrentUser == null)
            {
                DisableAllMenuItems();
                return;
            }

            // 根据用户角色设置权限
            string role = GlobalConfig.CurrentUser.UserRole?.ToLower() ?? "unknown";
            
            switch (role)
            {
                case "admin":
                    // 管理员拥有所有权限
                    break;

                case "hr":
                    // 人事只能管理部门和员工信息
                    menuSystem.Enabled = false;
                    // 启用部门和员工管理
                    break;

                case "finance":
                    // 财务只能管理工资相关信息
                    menuSystem.Enabled = false;
                    menuBasicInfo.Enabled = false;
                    // 启用工资管理
                    break;

                case "staff":
                    // 普通员工只能查询自己的工资
                    menuSystem.Enabled = false;
                    menuBasicInfo.Enabled = false;
                    menuAttendance.Enabled = false;
                    menuSalaryItem.Enabled = false;
                    menuSalaryPayment.Enabled = false;
                    // 只允许查询工资
                    break;

                default:
                    // 未知角色，禁用所有功能
                    DisableAllMenuItems();
                    break;
            }
        }

        private void DisableAllMenuItems()
        {
            menuSystem.Enabled = false;
            menuBasicInfo.Enabled = false;
            menuAttendance.Enabled = false;
            menuSalary.Enabled = false;
        }

        private void menuDepartment_Click(object sender, EventArgs e)
        {
            // 打开部门管理窗体
            using (DepartmentForm form = new DepartmentForm())
            {
                form.ShowDialog();
            }
        }

        private void menuEmployee_Click(object sender, EventArgs e)
        {
            // 打开员工管理窗体
            using (EmployeeForm form = new EmployeeForm())
            {
                form.ShowDialog();
            }
        }

        private void menuAttendance_Click(object sender, EventArgs e)
        {
            // 打开考勤管理窗体
            using (AttendanceForm form = new AttendanceForm())
            {
                form.ShowDialog();
            }
        }

        private void menuSalaryItem_Click(object sender, EventArgs e)
        {
            // 打开工资项目管理窗体
            using (SalaryItemForm form = new SalaryItemForm())
            {
                form.ShowDialog();
            }
        }

        private void menuSalaryCalculator_Click(object sender, EventArgs e)
        {
            // 打开工资计算窗体
            using (SalaryCalculatorForm form = new SalaryCalculatorForm())
            {
                form.ShowDialog();
            }
        }

        private void menuSalaryPayment_Click(object sender, EventArgs e)
        {
            // 打开工资发放窗体
            using (SalaryPaymentForm form = new SalaryPaymentForm())
            {
                form.ShowDialog();
            }
        }

        private void menuExit_Click(object sender, EventArgs e)
        {
            // 退出系统
            if (MessageBox.Show("确定要退出系统吗？", "确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                this.Close();
            }
        }
    }
} 